package com.hunan.api.catv.service.biz.member.login;

import cn.hutool.core.date.DateUtil;
import com.google.gson.JsonObject;
import com.hunan.api.catv.entity.ServiceResult;
import com.hunan.api.catv.entity.enums.LoginType;
import com.hunan.api.catv.entity.enums.SmsTypeEnum;
import com.hunan.api.catv.entity.po.Member;
import com.hunan.api.catv.entity.vo.MemberVo;
import com.hunan.api.catv.entity.vo.SystemDictVo;
import com.hunan.api.catv.service.ResultService;
import com.hunan.api.catv.service.biz.member.LoginService;
import com.hunan.api.catv.service.biz.member.MemberBiz;
import com.hunan.api.catv.service.biz.mq.MqBiz;
import com.hunan.api.catv.service.biz.system.SystemDictBiz;
import com.hunan.api.catv.service.biz.tencent_cloud.sms.SmsBiz;
import com.hunan.api.catv.service.biz.toupiao.TouPiaoBiz;
import com.hunan.api.catv.service.impl.member.MemberService;
import com.hunan.api.catv.utils.GsonUtils;
import com.hunan.api.catv.utils.RedisUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.Optional;

@Service
public class SmsLoginService extends ResultService implements LoginService {

    @Autowired
    MemberService memberService;

    @Autowired
    RedisUtils redisUtils;

    @Autowired
    SmsBiz smsBiz;

    @Autowired
    MemberBiz memberBiz;

    @Autowired
    TouPiaoBiz touPiaoBiz;

    @Autowired
    MqBiz mqBiz;

    @Autowired
    SystemDictBiz systemDictBiz;

    @Override
    public ServiceResult login(String id, String phone, String sms, String ip, String pushToken, String... extParams) {
        ServiceResult smsResult = smsBiz.checkSmsCode(SmsTypeEnum.LOGIN.getCode(), phone, sms);
        //TODO 苹果测试通过后删除这个if判断
        //-------------------------------------------
        Boolean testSwitch = Boolean.valueOf(Optional.ofNullable(systemDictBiz.info("AppleTestLoginSwitch")).orElse(new SystemDictVo()).getDictValue());
        if (testSwitch) {
            if ("123456".equals(sms) && "15675882044".equals(phone)) {
                smsResult = buildOK(true);
            }
        }
        //-------------------------------------------
        if (smsResult.getStatus()) {
            JsonObject memberJson = new JsonObject();
            MemberVo memberVo = memberBiz.getByPhone(phone);
            if (ObjectUtils.isEmpty(memberVo)) {
                ServiceResult registerResult = memberBiz.register(LoginType.sms.getCode(), null, phone, null, null, null, ip, pushToken);
                if (!registerResult.getStatus()) {
                    return registerResult;
                }
                memberVo = memberBiz.getByPhone(phone);
            } else if (0 != memberVo.getInviter() && 1 == memberVo.getInviteesStatus()) {
                Member member = new Member();
                member.setInviteesStatus(2);
                member.setMemberId(memberVo.getMemberId());
                member.setLastLoginTime(new Date());
                member.setFirstLoginTime(StringUtils.isBlank(DateUtil.formatDateTime(memberVo.getLastLoginTime())) ? new Date() : memberVo.getFirstLoginTime());
                ServiceResult updateResult = memberService.update(member);
                if (!updateResult.getStatus()) {
                    return updateResult;
                }
                touPiaoBiz.addTicketToUser(memberVo.getInviter(), 2);
            } else {
                Member member = new Member();
                member.setMemberId(memberVo.getMemberId());
                member.setLastLoginTime(new Date());
                member.setFirstLoginTime(StringUtils.isBlank(DateUtil.formatDateTime(memberVo.getLastLoginTime())) ? new Date() : memberVo.getFirstLoginTime());
                ServiceResult updateResult = memberService.update(member);
                if (!updateResult.getStatus()) {
                    return updateResult;
                }
            }
            memberJson.addProperty("isBind", false);
            memberJson.addProperty("member", GsonUtils.toJson(memberVo));
            return buildOK(memberJson, "");
        }
        return smsResult;
    }

    @Override
    public ServiceResult bind(String id, Integer loginType, Integer memberId) {
        return null;
    }
}
